package com.example.pagingdemo;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

@Database(entities = {Student.class}, version = 4, exportSchema = false)
public abstract class StudentsDatabase extends RoomDatabase {
    private static StudentsDatabase instance;

    static synchronized StudentsDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context, StudentsDatabase.class, "students_database")
                    .addMigrations(MIGRATION_1_2,MIGRATION_2_3,MIGRATION_3_4,MIGRATION_1_4)//对应实体的字段跟着增删
                    .build();
        }
        return instance;
    }

    abstract StudentDao getStudentDao();

    static final Migration MIGRATION_1_2=new Migration(1,2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE student_table ADD COLUMN age INTEGER NOT NULL DEFAULT 1");//添加一列 对应实体 get set
        }
    };
    static final Migration MIGRATION_2_3=new Migration(2,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE student_table ADD COLUMN weigth INTEGER NOT NULL DEFAULT 10");//添加一列 对应实体 get set
        }
    };
    static final Migration MIGRATION_3_4 = new Migration(3, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE student_table ADD COLUMN name TEXT");//添加一列 对应实体 get set
            //创建表
            database.execSQL(
                    "CREATE TABLE student_new (id INTEGER, student_age INTEGER, student_number INTEGER, PRIMARY KEY(student_id))");
            //复制表
            database.execSQL(
                    "INSERT INTO student_new (id, student_age, student_number) SELECT id, age, student_number FROM student_table");
            //删除表
            database.execSQL("DROP TABLE student_table");
            //修改表名称
            database.execSQL("ALTER TABLE student_new RENAME TO student_table");
        }
    };
    static final Migration MIGRATION_1_4 = new Migration(1, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE student_table ADD COLUMN age INTEGER NOT NULL DEFAULT 1");//添加一列 对应实体 get set
            database.execSQL("ALTER TABLE student_table ADD COLUMN name TEXT");//添加一列 对应实体 get set
            //创建表
            database.execSQL(
                    "CREATE TABLE student_new (id INTEGER, student_age INTEGER, student_number INTEGER, PRIMARY KEY(student_id))");
            //复制表
            database.execSQL(
                    "INSERT INTO student_new (id, student_age, student_number) SELECT id, age, student_number FROM student_table");
            //删除表
            database.execSQL("DROP TABLE student_table");
            //修改表名称
            database.execSQL("ALTER TABLE student_new RENAME TO student_table");
        }
    };
}
